package egovframework.npqs.pqi.mobile.activity;

import java.util.HashMap;

import com.sds.BizAppLauncher.gov.aidl.GovController;
import com.sds.BizAppLauncher.gov.aidl.GovController.IGovCallback;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.R;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ProdDetailItem;
import egovframework.npqs.pqi.mobile.jsonparser.ProdDetailThread;
import egovframework.npqs.pqi.mobile.jsonparser.QuarantineResultDetailThread;
import egovframework.npqs.pqi.mobile.jsonparser.QuarantineResultUpdateThread;
import egovframework.npqs.pqi.mobile.jsonparser.ProdDetailThread.ProdDetailThreadCallback;

public class ProdDetailStatusActivity extends Activity implements ProdDetailThreadCallback{
	private String resOffCd, resNo, resSeq;
	private AppStatus myapp;
	private Context context;
	
	private TextView titleTxt, navigation;
	private TextView content1_1, content2_1, content2_2, content3_1, content3_2, content4_1, content5_1, content5_2, content6_1, content8_1, content8_2;
	private TextView content9_1, content9_2, content10_1, content11_1, content11_2, content12_1, content13_1, content14_1, content16_1, content16_2;
	private TextView content17_1, content18_1, content19_1, content20_1, content20_2, content22_1, content23_1, content23_2, content24_1, content24_2;
	private TextView content25_1, content27_1, content27_2, content28_1, content28_2, content29_1, content29_2, content30_1, content31_1, content33_1;
	private TextView content33_2, content34_1, content34_2, content35_1, content36_1, content36_2, content37_1;
	private TextView content39_1, content39_2, content39_3, content39_4, content39_5, content39_6;
	
	private RelativeLayout detail_layout15, detail_layout16, detail_layout17, detail_layout18, detail_layout19, detail_layout20;	// 3번 영역
	private RelativeLayout detail_layout21, detail_layout22, detail_layout23, detail_layout24, detail_layout25;	// 4번 영역
	private RelativeLayout detail_layout26, detail_layout27, detail_layout28, detail_layout29, detail_layout30, detail_layout31;	// 5번 영역
	private RelativeLayout detail_layout32, detail_layout33, detail_layout34, detail_layout35, detail_layout36, detail_layout37;	// 6번 영역
	
	private ImageButton btnHome, btn_exit;
	
	private ProdDetailItem prodItem;
	private ProdDetailThread prodThread;
	
	private final static int MSG_QUARANTINE_COMPLETED = 0;
	private final static int MSG_QUARANTINE_ERROR = 1;
	private final static int MSG_NETWORK_ERROR = 2;
	
	private String mResult;
	
	/* G-MOBILE 변수*/
    private String temp;
    private HashMap<String,String>	searchMap;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppStatus.navigation.addPathLocation("품목결과상세현황");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_prod_status_detail);
		
		Intent intent = getIntent();
		resOffCd = intent.getStringExtra("resOffCd");
		resNo = intent.getStringExtra("resNo");
		resSeq = intent.getStringExtra("resSeq");
		
		init();
	}
	
	private void init(){
		context = this;
		myapp = (AppStatus) context.getApplicationContext();
		myapp.startBroker(context);
		
		titleTxt = (TextView)findViewById(R.id.headerTitleText);
		titleTxt.setText("품목결과상세현황");
		
		navigation = (TextView)findViewById(R.id.navigation);
		navigation.setText(AppStatus.navigation.getPath());
		Log.i("AppStatus.navigation.getPath()->", AppStatus.navigation.getPath());
		
		content1_1 = (TextView)findViewById(R.id.content_1_1);
		content2_1 = (TextView)findViewById(R.id.content_2_1);
		content2_2 = (TextView)findViewById(R.id.content_2_2);
		content3_1 = (TextView)findViewById(R.id.content_3_1);
		content3_2 = (TextView)findViewById(R.id.content_3_2);
		content4_1 = (TextView)findViewById(R.id.content_4_1);
		content5_1 = (TextView)findViewById(R.id.content_5_1);
		content5_2 = (TextView)findViewById(R.id.content_5_2);
		content6_1 = (TextView)findViewById(R.id.content_6_1);
		content8_1 = (TextView)findViewById(R.id.content_8_1);
		content8_2 = (TextView)findViewById(R.id.content_8_2);
		content9_1 = (TextView)findViewById(R.id.content_9_1);
		content9_2 = (TextView)findViewById(R.id.content_9_2);
		content10_1 = (TextView)findViewById(R.id.content_10_1);
		content11_1 = (TextView)findViewById(R.id.content_11_1);
		content11_2 = (TextView)findViewById(R.id.content_11_2);
		content12_1 = (TextView)findViewById(R.id.content_12_1);
		content13_1 = (TextView)findViewById(R.id.content_13_1);
		content14_1 = (TextView)findViewById(R.id.content_14_1);
		content16_1 = (TextView)findViewById(R.id.content_16_1);
		content16_2 = (TextView)findViewById(R.id.content_16_2);
		content17_1 = (TextView)findViewById(R.id.content_17_1);
		content18_1 = (TextView)findViewById(R.id.content_18_1);
		content19_1 = (TextView)findViewById(R.id.content_19_1);
		content20_1 = (TextView)findViewById(R.id.content_20_1);
		content20_2 = (TextView)findViewById(R.id.content_20_2);
		content22_1 = (TextView)findViewById(R.id.content_22_1);
		content23_1 = (TextView)findViewById(R.id.content_23_1);
		content23_2 = (TextView)findViewById(R.id.content_23_2);
		content24_1 = (TextView)findViewById(R.id.content_24_1);
		content24_2 = (TextView)findViewById(R.id.content_24_2);
		content25_1 = (TextView)findViewById(R.id.content_25_1);
		content27_1 = (TextView)findViewById(R.id.content_27_1);
		content27_2 = (TextView)findViewById(R.id.content_27_2);
		content28_1 = (TextView)findViewById(R.id.content_28_1);
		content28_2 = (TextView)findViewById(R.id.content_28_2);
		content29_1 = (TextView)findViewById(R.id.content_29_1);
		content29_2 = (TextView)findViewById(R.id.content_29_2);
		content30_1 = (TextView)findViewById(R.id.content_30_1);
		content31_1 = (TextView)findViewById(R.id.content_31_1);
		content33_1 = (TextView)findViewById(R.id.content_33_1);
		content33_2 = (TextView)findViewById(R.id.content_33_2);
		content34_1 = (TextView)findViewById(R.id.content_34_1);
		content34_2 = (TextView)findViewById(R.id.content_34_2);
		content35_1 = (TextView)findViewById(R.id.content_35_1);
		content36_1 = (TextView)findViewById(R.id.content_36_1);
		content36_2 = (TextView)findViewById(R.id.content_36_2);
		content37_1 = (TextView)findViewById(R.id.content_37_1);
		content39_1 = (TextView)findViewById(R.id.content_39_1);
		content39_2 = (TextView)findViewById(R.id.content_39_2);
		content39_3 = (TextView)findViewById(R.id.content_39_3);
		content39_4 = (TextView)findViewById(R.id.content_39_4);
		content39_5 = (TextView)findViewById(R.id.content_39_5);
		content39_6 = (TextView)findViewById(R.id.content_39_6);
		
		detail_layout15 = (RelativeLayout)findViewById(R.id.detail_layout15);
		detail_layout16 = (RelativeLayout)findViewById(R.id.detail_layout16);
		detail_layout17 = (RelativeLayout)findViewById(R.id.detail_layout17);
		detail_layout18 = (RelativeLayout)findViewById(R.id.detail_layout18);
		detail_layout19 = (RelativeLayout)findViewById(R.id.detail_layout19);
		detail_layout20 = (RelativeLayout)findViewById(R.id.detail_layout20);
		
		detail_layout21 = (RelativeLayout)findViewById(R.id.detail_layout21);
		detail_layout22 = (RelativeLayout)findViewById(R.id.detail_layout22);
		detail_layout23 = (RelativeLayout)findViewById(R.id.detail_layout23);
		detail_layout24 = (RelativeLayout)findViewById(R.id.detail_layout24);
		detail_layout25 = (RelativeLayout)findViewById(R.id.detail_layout25);
		
		detail_layout26 = (RelativeLayout)findViewById(R.id.detail_layout26);
		detail_layout27 = (RelativeLayout)findViewById(R.id.detail_layout27);
		detail_layout28 = (RelativeLayout)findViewById(R.id.detail_layout28);
		detail_layout29 = (RelativeLayout)findViewById(R.id.detail_layout29);
		detail_layout30 = (RelativeLayout)findViewById(R.id.detail_layout30);
		detail_layout31 = (RelativeLayout)findViewById(R.id.detail_layout31);
		
		detail_layout32 = (RelativeLayout)findViewById(R.id.detail_layout32);
		detail_layout33 = (RelativeLayout)findViewById(R.id.detail_layout33);
		detail_layout34 = (RelativeLayout)findViewById(R.id.detail_layout34);
		detail_layout35 = (RelativeLayout)findViewById(R.id.detail_layout35);
		detail_layout36 = (RelativeLayout)findViewById(R.id.detail_layout36);
		detail_layout37 = (RelativeLayout)findViewById(R.id.detail_layout37);

		btn_exit = (ImageButton)findViewById(R.id.btn_exit);
		btn_exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		btnHome = (ImageButton)findViewById(R.id.btnHome);
		btnHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myapp.setAbleHome(true);
				finish();
			}
		});
		
		searchMap = new HashMap<String,String>();
		
		searchMap.put("resOffCd", resOffCd);
		searchMap.put("resNo", resNo);
		searchMap.put("resSeq", resSeq);
		
		myapp.showDialogNo(context);
		temp = "처음검색";
		//asyncTaskIsConnectedVPN();
		threadStart();
		
	}
	
	public void threadStart(){
		if("처음검색".equals(temp)){
    		prodThread = new ProdDetailThread(context, this, searchMap, AppStatus.getmUserId());
    		prodThread.start();
    	}
	}
	
	private void layoutSetting(){
		content1_1.setText(prodItem.getAllInsttNm());
		content2_1.setText(prodItem.getFullRecNo());
		content2_2.setText(prodItem.getReqstDate());
		content3_1.setText(prodItem.getNatNm());
		content3_2.setText(prodItem.getTraDe());
		content4_1.setText(prodItem.getVenNm());
		content5_1.setText(prodItem.getTrnNm());
		content5_2.setText(prodItem.getMetNm());
		content6_1.setText(prodItem.getFileList());
		content8_1.setText(prodItem.getFullResNo());
		content8_2.setText(prodItem.getValidflagNm());
		content9_1.setText(prodItem.getFieDe());
		content9_2.setText(prodItem.getResultProgrsNm());
		content10_1.setText(prodItem.getFieArea());
		content11_1.setText(prodItem.getAftDsuseTrgetNm());
		content11_2.setText(prodItem.getDspsTrgetNm());
		content12_1.setText(prodItem.getQuranNm1() + " " + prodItem.getQuranNm2());
		content13_1.setText(prodItem.getDrafLastDe());
		content14_1.setText(prodItem.getRemark());
		
		if(prodItem.getData3().equals("Y")){
			content16_1.setText(prodItem.getLabDe());
			//content16_2.setText(prodItem.getLabQuranNm());
			content17_1.setText(prodItem.getLabQuranNm());
			content18_1.setText(prodItem.getLabEqu());
			content19_1.setText(prodItem.getLabOpin());
			content20_1.setText(prodItem.getPassYn());
			content20_2.setText(prodItem.getStateTot());	
		}else{
			detail_layout15.setVisibility(View.GONE);
			detail_layout16.setVisibility(View.GONE);
			detail_layout17.setVisibility(View.GONE);
			detail_layout18.setVisibility(View.GONE);
			detail_layout19.setVisibility(View.GONE);
			detail_layout20.setVisibility(View.GONE);
		}
		
		if(prodItem.getData4().equals("Y")){
			content22_1.setText(prodItem.getDbyhsNm());
			content23_1.setText(prodItem.getProNm());
			content23_2.setText(prodItem.getDamDegNm());
			content24_1.setText(prodItem.getRegNm());
			content24_2.setText(prodItem.getSamNoVeiw());
			content25_1.setText(prodItem.getOrgPla());
		}else{
			detail_layout21.setVisibility(View.GONE);
			detail_layout22.setVisibility(View.GONE);
			detail_layout23.setVisibility(View.GONE);
			detail_layout24.setVisibility(View.GONE);
			detail_layout25.setVisibility(View.GONE);
		}
		
		if(prodItem.getData5().equals("Y")){
			content27_1.setText(prodItem.getFullDsnfResN0());
			content27_2.setText(prodItem.getPasDe());
			content28_1.setText(prodItem.getAntDatefr() + " ~ " + prodItem.getAntDateto());
			//content28_2.setText(prodItem.getFullDnsfCmmdNo());
			content29_1.setText(prodItem.getFullDnsfCmmdNo());
			content29_2.setText(prodItem.getDnsfCmmDe());
			content30_1.setText(prodItem.getSanghos());
			content31_1.setText(prodItem.getAntArea());
		}else{
			detail_layout26.setVisibility(View.GONE);
			detail_layout27.setVisibility(View.GONE);
			detail_layout28.setVisibility(View.GONE);
			detail_layout29.setVisibility(View.GONE);
			detail_layout30.setVisibility(View.GONE);
			detail_layout31.setVisibility(View.GONE);
		}
		
		if(prodItem.getData6().equals("Y")){
			content33_1.setText(prodItem.getFullDsuseResNo());
			content33_2.setText(prodItem.getWasDe());
			content34_1.setText(prodItem.getActDe());
			//content34_2.setText("");
			content35_1.setText(prodItem.getWorArea());
			content36_1.setText(prodItem.getFullDsuseCmmdNo());
			content36_2.setText(prodItem.getDsuseCmmdDe());
			content37_1.setText(prodItem.getSelDsuseNm());
		}else{
			detail_layout32.setVisibility(View.GONE);
			detail_layout33.setVisibility(View.GONE);
			detail_layout34.setVisibility(View.GONE);
			detail_layout35.setVisibility(View.GONE);
			detail_layout36.setVisibility(View.GONE);
			detail_layout37.setVisibility(View.GONE);
		}
		
		content39_1.setText(prodItem.getRecSeq());
		content39_2.setText(prodItem.getPrdlstCode());
		content39_3.setText(prodItem.getItmQty());
		content39_4.setText(prodItem.getItemType());
		content39_5.setText(prodItem.getPrdlstNm());
		content39_6.setText(prodItem.getInsQty());
	}

	@Override
	public void OnProdDetailCompleted(boolean isNetworkConntected,
			ProdDetailItem prodItem, String message) {
		if(null != message && !"".equals(message)){
			mResult = message;
			reesultToast();
		}
		
		if(myapp.dialog !=null && myapp.dialog.isShowing()) {
			myapp.dialog.dismiss();
		}
		
		if(!isNetworkConntected){	// 네트워크 접속이 되어 있지 않을 때
			Message msg = handler.obtainMessage();
			msg.what = MSG_NETWORK_ERROR;
			handler.sendMessage(msg);
			return;
		}
		
		if(prodItem != null) {
			this.prodItem = prodItem;
			Message msg = handler.obtainMessage();
			msg.what = MSG_QUARANTINE_COMPLETED;
			handler.sendMessage(msg);
		}
	}

	@Override
	public void OnProdDetailError(ErrorItem errorItem) {
		if(myapp.dialog !=null && myapp.dialog.isShowing()) {
			myapp.dialog.dismiss();
		}

		Message msg = handler.obtainMessage();
		msg.what = MSG_QUARANTINE_ERROR;
		if(errorItem != null) {
			msg.obj = errorItem.getrDesc();
		}
		handler.sendMessage(msg);
		
	}
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(final Message msg) {
			switch (msg.what) {
			case MSG_QUARANTINE_COMPLETED:
				layoutSetting();
				break;

			case MSG_QUARANTINE_ERROR:
				String errMsg = "";
				if(msg == null ||msg.obj.toString().equals("")){
					errMsg = "서버와의 연결에 실패하였습니다.";
				}else{
					errMsg = msg.obj.toString();	
				}
				myapp.utilm.ShowDialog(ProdDetailStatusActivity.this, "실패", errMsg, 20);
				break;

			case MSG_NETWORK_ERROR:
				myapp.utilm.ShowDialog(ProdDetailStatusActivity.this, "실패", "네트워크에러", 20);

			default:
				break;
			}
		}
	};
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		AppStatus.navigation.delPathLocation();
	}
	
	private void reesultToast() {
		runOnUiThread(new Runnable() {

            public void run() {
                Toast toast = Toast.makeText(context, mResult, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 50);
                toast.show();
            }
        });
    }
}